﻿using HslCommunication.Enthernet;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SYSoft.Comm.Utils;
using SYSoft.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SYSoft.Controllers.DeviceManage
{
    public class DeviceSwitchController : BaseController
    {
        public DeviceSwitchController()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="model">0 关闭  1 开</param>
        /// <param name="stationid"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        [HttpPost]
        public  MessageModel<bool[]> PostSwitch(int deviceid, int model,int index)
        {
            NetSimplifyClient simplifyClient = new NetSimplifyClient("127.0.0.1", 1234);
            simplifyClient.ReceiveTimeOut = 5000;

            var res = simplifyClient.ReadCustomerFromServer(model, JObject.FromObject(new { IpAddress=ServerInfo.deviceSwitchs[deviceid][0].IpAddress, StationId = 1, Index = index }).ToString());
            if (res.IsSuccess)
            {
                if (res.Content1 == 1)
                {
                    JObject obj2 = JObject.Parse(res.Content2);
                    JArray array = JArray.Parse(obj2["data"].ToString());
                    bool[] data = array.ToObject<bool[]>();

                    return MessageModel.CreateSuccessResult(data);

                }
                else
                {
                    return MessageModel.CreateFailedResult<bool[]>("指令下发失败！！" + res.Content2);
                }
            }
            else
            {
                return MessageModel.CreateFailedResult<bool[]>("指令下发失败！！" + "请检查网络" + res.Message);
            }

                   
        }


        [HttpGet]
        public MessageModel<List<string>> GetAllSwitch()
        {
            MysqlHelper mysql = new MysqlHelper();
            string sql = "select * from device_switch_config";
            var dt = mysql.ExecuteQuery(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                List<string> list = new List<string>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    list.Add(dt.Rows[i]["PassName"].ToString());
                }
                return MessageModel.CreateSuccessResult(list);
            }
            else
            {
                return new MessageModel<List<string>>();
            }
        }


    }
}
